package per.zqx.msrw.config;

import lombok.extern.slf4j.Slf4j;
import per.zqx.msrw.enums.DBEnum;

/**
 * DBContextHolder, 数据源切换
 *
 * @author Sa, Zhang 2021-12-28 12:39
 */
@Slf4j
public class DBContextHolder {
    private static final ThreadLocal<DBEnum> contextHolder = new ThreadLocal<>();

    /**
     * 注意返回的是枚举类型，不是 ThreadLocal !!!
     * @return DBEnum
     */
    public static DBEnum getContextHolder() {
        return contextHolder.get();
    }

    public static void set(DBEnum dbEnum){
        contextHolder.set(dbEnum);
    }

    public static void master(){
        set(DBEnum.MASTER);
        System.out.println("************ Master Write Operation ************");
    }
    public static void slave(){
        set(DBEnum.SLAVE);
        System.out.println("************ Slave Read Operation ************");
    }

    public static void clear(){
        contextHolder.remove();
    }
}
